<?php
/**
 * Description:
 * Author: JennyWang
 * Date: 2018/9/1
 * Time: 15:44
 * Created by PhpStorm.
 */

namespace App\Services\Repositories;


use App\Http\Models\Admin\ProductCategory;
use App\Services\Utils\ApiChecker;
use Illuminate\Database\Eloquent\Collection;

class ProductCategoryRepository
{
    use ApiChecker;
    
    protected $productCategory;
    
    public function __construct()
    {
        $this->productCategory = new ProductCategory();
    }
    
    /**
     * @return mixed
     */
    public function indexWithSub()
    {
        /** @var Collection $categories */
        $categories = ProductCategory::where('super_id', ProductCategory::TOP_CATEGORY)
            ->with('subCategories')
            ->latest('priority')
            ->get();
    
        $categories->each(function (ProductCategory $cate){
            $cate->subCategories->each(function (ProductCategory $sub_cate){
                $sub_cate->link = url('/production') . '?pid=' . $sub_cate->getKey();
            });
        });
    
        return $categories;
    }
}